/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.AttributeModel;
import com.elf.gwt.common.client.model.AttributeValueModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.ListBox;

/**
 *  
 */
public class ProductOptionsToValuesComponent extends EditTableComponent
{	
	protected static ProductOptionsComponent productOptionsComponent = null;
	protected static ProductOptionsValuesComponent productOptionsValuesComponent = null;
	protected List<AttributeModel> optionModels;
	protected List<AttributeValueModel> optionValueModels;
	protected AttributeValueModel emptyAttributeValueModel;

	/**
	 * Constructor
	 */
	public ProductOptionsToValuesComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);

		emptyAttributeValueModel = new AttributeValueModel();
		optionModels = new ArrayList<AttributeModel>();
		optionValueModels = new ArrayList<AttributeValueModel>();
		if (productOptionsComponent == null) productOptionsComponent = (ProductOptionsComponent)controller.getComponent("ProductOptionsComponent");
		if (productOptionsValuesComponent == null) productOptionsValuesComponent = (ProductOptionsValuesComponent)controller.getComponent("ProductOptionsValuesComponent");
	}
	
	protected void buildMetaModel(JSONObject jsonObject)
	{
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		optionModels.clear();
		optionValueModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		AttributeValueModel model = new AttributeValueModel(jsonObject);
		optionValueModels.add(model);
		
		int optionsSz = optionModels.size() -1;
		AttributeModel attributeModel = null;
		if (optionsSz > -1) attributeModel = optionModels.get(optionModels.size() -1);
		
		if (attributeModel == null || attributeModel.getId() != model.getAttributeId())
		{
			attributeModel = new AttributeModel(model.getAttributeId(), model.getAttributeName());
			optionModels.add(attributeModel);
		}
		attributeModel.addValues(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = optionValueModels.size();
		if (edit)
			numberOfTableColumns = 3;
		else 
			numberOfTableColumns = 2;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Option");
		grid.setText(0, 1, "Value");
		if (edit) grid.setText(0, 2, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, optionValueModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyAttributeValueModel);
	}

	public void displayRow(Grid grid, int row, AttributeValueModel model) {
		if (edit) {
			Hidden idHidden = new Hidden();
			idHidden.setName("optionToValuesId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("optionToValuesId-" + row, idHidden);
			
			List<AttributeModel> optionModels = productOptionsComponent.getOptionModels();
			int i = 0;
			ListBox optionListBox = new ListBox();
			optionListBox.addItem("Select ...", "0");
			if (model.getAttributeId() < 1)
			{
				optionListBox.setSelectedIndex(i);
			}
			i++;
			Iterator<AttributeModel> iter1 = optionModels.iterator();
			while(iter1.hasNext())
			{
				AttributeModel omodel = iter1.next();
				optionListBox.addItem(omodel.getName(), String.valueOf(omodel.getId()));
				if (omodel.getId() == model.getAttributeId())
				{
					optionListBox.setSelectedIndex(i);
				}
				i++;
			}
			elements.put("optionToValuesOptionId-" + row, optionListBox);
			grid.setWidget(row, 0, optionListBox);
			
			List<AttributeModel> optionValueModels = productOptionsValuesComponent.getOptionValueModels();
			i = 0;
			ListBox optionValueListBox = new ListBox();
			optionValueListBox.addItem("Select ...", "0");
			if (model.getAttributeId() < 1)
			{
				optionValueListBox.setSelectedIndex(i);
			}
			i++;
			iter1 = optionValueModels.iterator();
			while(iter1.hasNext())
			{
				AttributeModel omodel = iter1.next();
				optionValueListBox.addItem(omodel.getName(), String.valueOf(omodel.getId()));
				if (omodel.getId() == model.getAttributeId())
				{
					optionValueListBox.setSelectedIndex(i);
				}
				i++;
			}
			elements.put("optionToValuesOptionValueId-" + row, optionValueListBox);
			grid.setWidget(row, 1, optionValueListBox);
			
			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("optionToValuesDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 2, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, model.getAttributeName());
			grid.setText(row, 1, model.getName());
		}
	}

	public List<AttributeModel> getOptionModels() {
		return optionModels;
	}
}
